package com.data.config.init.pdf;

import com.data.common.constant.enums.ContractEnum;
import com.data.common.constant.enums.ProfileEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.ObjectUtils;

import java.io.File;
import java.util.Arrays;

/**
 * <p>
 * PdfProperties
 * </p>
 *
 * @author li
 * @version 1.0
 * @date 2022-01-14 14:28
 * @since 1.0
 */
@Data
@ConfigurationProperties(prefix = "databoot.path")
@Slf4j
public class BootFileProperties implements InitializingBean {

    @Autowired
    private ConfigurableApplicationContext configurableApplicationContext;

    /**
     * webapp文件路径
     */
    private String webapp;

    /**
     * 文件上传根目录 设置
     */
    private String uploadUrl;

    /**
     * 合同 path
     */
    private Contract contract = new Contract();

    /**
     * 字体 path
     */
    private Font font = new Font();

    /**
     * 日志 path
     */
    private Log log = new Log();

    /**
     * opencv face path
     */
    private Opencv opencv = new Opencv();

    @Override
    public void afterPropertiesSet() throws Exception {
        ConfigurableEnvironment environment = this.configurableApplicationContext.getEnvironment();
        String[] activeProfiles = environment.getActiveProfiles();
        if (ObjectUtils.isEmpty(activeProfiles)) {
            activeProfiles = environment.getDefaultProfiles();
        }
        if (Arrays.asList(activeProfiles).contains(ProfileEnum.local.getDesc()) || Arrays.asList(activeProfiles).contains(ProfileEnum.dev.getDesc())) {
            font.setFontPath(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.FONT_PATH.getMsg()));
            contract.setTemplatePath(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.CONTRACT_PATH.getMsg()));
            contract.setTemplatePathFixed(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.CONTRACT_PATH_FIXED.getMsg()));

            opencv.setEye(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.EYE_PATH.getMsg()));
            opencv.setFacePath(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.FACE_PATH.getMsg()));
            opencv.setLexiconPath(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.OCR_PATH.getMsg()));
            opencv.setDllPath(System.getProperty(ProfileEnum.default_dir.getDesc()).concat(ContractEnum.DLL_PATH.getMsg()));

        }

    }

    @Data
    public static class Contract {
        private String templatePath;
        private String templatePathFixed;

        public String getTemplatePath(String address) {
            if (StringUtils.isNotBlank(address) && address.length() > 27) {
                return templatePathFixed;
            } else {
                return templatePathFixed;
            }
        }
    }

    @Data
    public static class Opencv {
        private String eye;
        private String facePath;
        private String dllPath;
        private String lexiconPath;

    }

    @Data
    public static class Font {
        private String fontPath;
    }

    @Data
    public static class Log {
        private String dbPath = "";
        private String eventType = "错误请求";
    }


}
